model{
  for (i in 1:n){
    for (j in 1:p){
      Y[i, j] ~ dcat(prob[i, j, 1:K[j]])
    }
    theta[i] ~ dnorm(0.0, 1.0)

    ## Cumulative probabilities for item categories
    for (j in 1:p){
      for (k in 1:(K[j]-1)){
        logit(P[i, j, k]) <- kappa[j, k] - alpha[j]*theta[i]
      }
      P[i, j, K[j]] <- 1.0
    }

    ## Item category probabilities
    for (j in 1:p){
      prob[i, j, 1] <- P[i, j, 1]
      for (k in 2:K[j]){
        prob[i, j, k] <- P[i, j, k] - P[i, j, k-1]
      }
    }
  }

 ## Priors on item parameters
  for (j in 1:p){
    alpha[j] ~ dnorm(m.alpha, pr.alpha) T(0, )
  }
  pr.alpha <- pow(s.alpha, -2)

  ## Priors on thresholds
  for (j in 1:p){
    for (k in 1:(K[j]-1)){
      kappa.star[j, k] ~ dnorm(m.kappa, pr.kappa)
    }
    kappa[j, 1:(K[j]-1)] <- sort(kappa.star[j, 1:(K[j]-1)])
  }
  pr.kappa <- pow(s.kappa, -2)
}
